﻿using B.S.Meeting.Domain;
using B.S.Meeting.ErrorCode;
using B.S.Meeting.Infrastructure.Interfaces;
using B.S.Meeting.WriteAPI.Applications.Commands.MeetingRooms;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.Meeting.WriteAPI.Applications.CommandHandlers.MeetingRooms
{
    public class AddRoomCommandHandler : IRequestHandler<AddRoomCommand, APIResult<int>>
    {
        IBaseRepository<MeetingRoom> mrRep;
        ILogger<AddRoomCommandHandler> logger;

        public AddRoomCommandHandler(IBaseRepository<MeetingRoom> mrRep, ILogger<AddRoomCommandHandler> logger)
        {
            this.mrRep = mrRep;
            this.logger = logger;
        }

        public async Task<APIResult<int>> Handle(AddRoomCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 检查请求参数是否有效
                if (request.Data == null || string.IsNullOrWhiteSpace(request.Data.MeetingRoomName))
                {
                    logger.LogWarning("会议室数据无效或名称为空");
                    return new APIResult<int>
                    {
                        Code = StatusEnumCode.Fail,
                        Message = "会议室数据无效或名称为空",
                        Data = 0
                    };
                }

                // 检查是否已存在同名会议室
                var existingRoom = await mrRep.GetAll().FirstOrDefaultAsync(r => r.MeetingRoomName == request.Data.MeetingRoomName, cancellationToken);
                if (existingRoom != null)
                {
                    logger.LogWarning($"会议室 '{request.Data.MeetingRoomName}' 已存在");
                    return new APIResult<int>
                    {
                        Code = StatusEnumCode.Fail,
                        Message = "会议室已存在",
                        Data = 0
                    };
                }

                // 创建新的会议室实体
                var newRoom = new MeetingRoom
                {
                    MeetingRoomName = request.Data.MeetingRoomName,
                    Capacity = request.Data.Capacity,
                    Location = request.Data.Location,
                    UserName=request.Data.UserName,
                    UserPhone=request.Data.UserPhone,
                    CheckStatus = request.Data.CheckStatus,
                    IsIntelligent=request.Data.IsIntelligent,
                    MeetingRoomImage=request.Data.MeetingRoomImage,
                    IsDel=false,
                };

                // 添加到数据库
                await mrRep.AddAsync(newRoom);

                logger.LogInformation($"成功添加会议室: {newRoom.MeetingRoomName} (ID: {newRoom.MeetingRoomId})");

                // 返回成功结果
                return new APIResult<int>
                {
                    Code = StatusEnumCode.Ok,
                    Message = "添加会议室成功",
                    Data = 1
                };
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加会议室时发生错误");
                throw;
            }
        }
    }
}
